<?php
/**
---------------------------------------
Table cate_tin_tuc
---------------------------------------
*/

/**
count rows table cate_tin_tuc
*/
function getRowsTypeNew() {
	$con = new ConnectDB();
	
	$sql = "Select COUNT(*) FROM cate_tin_tuc WHERE del_flg='0'";
	return $con -> getvalueString($sql);
}

/**
get table cate_tin_tuc limit
*/
function getTypeNewsLimit($start, $lenght) {
	$con = new ConnectDB();
	$sql = "Select cat_code, cat_name, date_create, date_update, isuse FROM cate_tin_tuc WHERE del_flg='0' ORDER BY date_update DESC LIMIT ".$start.",".$lenght;
	return $con -> getvalueString($sql);
}

/**
get table cate_tin_tuc
*/
function getTypeNews() {
	$con = new ConnectDB();
	$tbl = "cate_tin_tuc";
	$fn = array('cat_code', 'cat_name', 'date_create', 'date_update');
	$condetion = array('isuse' => '1', 'del_flg' => '0');
	return $con -> getvalue($tbl, $fn, $condetion);
}

/**
get table cate_tin_tuc by id
*/
function getTypeNewByID($id) {
	$con = new ConnectDB();
	$tbl = "cate_tin_tuc";
	$fn = array('cat_code', 'cat_name', 'date_create', 'date_update', 'isuse');
	$condetion = array('cat_code' => $id, 'del_flg' => '0');
	return $con -> getvalue($tbl, $fn, $condetion);
}

/**
insert table cate_tin_tuc
*/
function insertTypeNew($id, $name) {
	$con = new ConnectDB();
	$tbl = "cate_tin_tuc";
	$fn = array('cat_code' => $id, 'cat_name' => $name, 'date_create' => time(), 'date_update' => time());
	return $con -> insert($tbl, $fn);
}

/**
display table cate_tin_tuc for id
*/
function displayTypeNew($id, $isuse) {
	$con = new ConnectDB();
	$tbl = "cate_tin_tuc";
	$fn = array('isuse' => $isuse);
	$conf = array('cat_code' => $id);
	return $con -> update($tbl, $fn, $conf);
}
/**
update display list id
*/
function displayTypeNewList($ids, $isuse) {
	$con = new ConnectDB();
	$tbl = "cate_tin_tuc";
	$fn = array('isuse' => $isuse);
	$rowid = 'cat_code';
	return $con -> updatedels($tbl, $fn, $rowid, $ids);
}

/**
delete table cate_tin_tuc for list id
*/
function deleteListTypeNew($ids) {
	$con = new ConnectDB();
	$tbl = "cate_tin_tuc";
	$fn = array('del_flg' => '1');
	$rowid = 'cat_code';
	return $con -> updatedels($tbl, $fn, $rowid, $ids);
}

/**
delete table cate_tin_tuc for id
*/
function deleteTypeNew($id) {
	$con = new ConnectDB();
	$tbl = "cate_tin_tuc";
	$fn = array('del_flg' => '1');
	$conf = array('cat_code' => $id);
	return $con -> update($tbl, $fn, $conf);
}

/**
update cate_tin_tuc
*/
function updateTypeNew($id, $name) {
	$con = new ConnectDB();
	$tbl = "cate_tin_tuc";
	$fn = array('cat_name' => $name, 'date_update' => time());
	$conf = array('cat_code' => $id);
	return $con -> update($tbl, $fn, $conf);
}


/**
---------------------------------------
Table tin_tuc
---------------------------------------
*/

/**
count rows table tin_tuc
*/
function getRowsNews($type) {
	$con = new ConnectDB();
	
	$sql = "Select COUNT(*) FROM tin_tuc as d, cate_tin_tuc as c WHERE c.cat_code = d.cat_code ";
	if($type != "") {
		$sql .= "AND d.cat_code='".$type."' ";
	}
	$sql .= "AND d.del_flg='0' AND c.del_flg='0' AND c.isuse='1'";
	return $con -> getvalueString($sql);
}

/**
get table tin_tuc limit
*/
function getNewsLimit($type, $start, $lenght) {
	$con = new ConnectDB();
	$sql = "Select d.pro_code, d.cat_code, c.cat_name, d.title, d.link, d.content_tt, d.tac_gia, d.date_create, d.date_update, d.isuse FROM tin_tuc as d, cate_tin_tuc as c WHERE c.cat_code = d.cat_code ";
	if($type != "") {
		$sql .= "AND d.cat_code='".$type."' ";
	}
	$sql .= "AND d.del_flg='0' AND c.del_flg='0' AND c.isuse='1' ORDER BY d.date_update DESC LIMIT ".$start.",".$lenght;
	return $con -> getvalueString($sql);
}

/**
get table tin_tuc
*/
function getNews($type) {
	$con = new ConnectDB();
	$sql = "Select d.pro_code, d.cat_code, c.cat_name, d.title, d.link, d.content_tt, d.tac_gia, d.date_create, d.date_update, d.isuse WHERE FROM tin_tuc as d, cate_tin_tuc as c WHERE c.cat_code = d.cat_code ";
	if($type != "") {
		$sql .= "AND d.cat_code='".$type."' ";
	}
	$sql .= "AND d.del_flg='0' AND c.del_flg='0' AND c.isuse='1' ORDER BY d.date_update DESC";
	return $con -> getvalueString($sql);
}

/**
get table tin_tuc by id
*/
function getNewsByID($id) {
	$con = new ConnectDB();
	$sql = "Select d.pro_code, d.cat_code, c.cat_name, d.title, d.link, d.content_tt, d.content, d.tac_gia, d.date_create, d.date_update, d.isuse FROM tin_tuc as d, cate_tin_tuc as c WHERE d.pro_code = '".$id."' AND c.cat_code = d.cat_code AND d.del_flg='0' AND c.del_flg='0' AND c.isuse='1'";
	return $con -> getvalueString($sql);
}

/**
insert table tin_tuc
*/
function insertNews($id, $type, $title, $link, $content_tt, $content, $tac_gia) {
	$con = new ConnectDB();
	$tbl = "tin_tuc";
	$fn = array('pro_code' => $id, 'cat_code' => $type, 'title' => $title, 'link' => $link, 'content_tt' => $content_tt, 'content' => $content, 'tac_gia' => $tac_gia, 'date_create' => time(), 'date_update' => time());
	return $con -> insert($tbl, $fn);
}

/**
display table tin_tuc for id
*/
function displayNews($id, $isuse) {
	$con = new ConnectDB();
	$tbl = "tin_tuc";
	$fn = array('isuse' => $isuse);
	$conf = array('pro_code' => $id);
	return $con -> update($tbl, $fn, $conf);
}
/**
update display list id
*/
function displayNewsList($ids, $isuse) {
	$con = new ConnectDB();
	$tbl = "tin_tuc";
	$fn = array('isuse' => $isuse);
	$rowid = 'pro_code';
	return $con -> updatedels($tbl, $fn, $rowid, $ids);
}

/**
delete table tin_tuc for list id
*/
function deleteListNews($ids) {
	$con = new ConnectDB();
	$tbl = "tin_tuc";
	$fn = array('del_flg' => '1');
	$rowid = 'pro_code';
	return $con -> updatedels($tbl, $fn, $rowid, $ids);
}

/**
delete table tin_tuc for id
*/
function deleteNews($id) {
	$con = new ConnectDB();
	$tbl = "tin_tuc";
	$fn = array('del_flg' => '1');
	$conf = array('pro_code' => $id);
	return $con -> update($tbl, $fn, $conf);
}

/**
update tin_tuc
*/
function updateNews($id, $type, $title, $link, $content_tt, $content, $tac_gia) {
	$con = new ConnectDB();
	$tbl = "tin_tuc";
	$fn = array('cat_code' => $type, 'title' => $title, 'link' => $link, 'content_tt' => $content_tt, 'content' => $content, 'tac_gia' => $tac_gia, 'date_update' => time());
	$conf = array('pro_code' => $id);
	return $con -> update($tbl, $fn, $conf);
}


/**
INDEX
*/
/**
count rows table tin_tuc
*/
function getRowsNewsIndex($type) {
	$con = new ConnectDB();
	
	$sql = "Select COUNT(*) FROM tin_tuc as d, cate_tin_tuc as c WHERE c.cat_code = d.cat_code ";
	if($type != "") {
		$sql .= "AND d.cat_code='".$type."' ";
	}
	$sql .= "AND d.del_flg='0' AND c.del_flg='0' AND c.isuse='1' AND d.isuse='1'";
	return $con -> getvalueString($sql);
}

/**
get table tin_tuc limit
*/
function getNewsLimitIndex($type, $start, $lenght) {
	$con = new ConnectDB();
	$sql = "Select d.pro_code, d.cat_code, c.cat_name, d.title, d.link, d.content_tt, d.tac_gia, d.date_create, d.date_update, d.isuse FROM tin_tuc as d, cate_tin_tuc as c WHERE c.cat_code = d.cat_code ";
	if($type != "") {
		$sql .= "AND d.cat_code='".$type."' ";
	}
	$sql .= "AND d.del_flg='0' AND c.del_flg='0' AND c.isuse='1' AND d.isuse='1' ORDER BY d.date_update DESC LIMIT ".$start.",".$lenght;
	return $con -> getvalueString($sql);
}


/**
get products by id type product
*/
function getNewsLienQuan($idpro, $type) {
	$con = new ConnectDB();
	$sql = "Select d.pro_code, d.cat_code, c.cat_name, d.title, d.link, d.content_tt, d.tac_gia, d.date_create, d.date_update, d.isuse FROM tin_tuc as d, cate_tin_tuc as c WHERE c.cat_code = d.cat_code AND d.cat_code='".$type."' AND d.pro_code<>'".$idpro."' AND d.del_flg='0' AND c.del_flg='0' AND c.isuse='1' AND d.isuse='1' ORDER BY d.date_update DESC LIMIT 0,6";
	return $con -> getvalueString($sql);
}
?>